<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>从结果集中的下一行返回单独的一列。</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="pdostatement.fetchall.html">« PDOStatement::fetchAll</a></li>
      <li style="float: right;"><a href="pdostatement.fetchobject.html">PDOStatement::fetchObject »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.pdostatement.html">PDOStatement</a></li>
    <li>从结果集中的下一行返回单独的一列。</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="pdostatement.fetchcolumn" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDOStatement::fetchColumn</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8, PECL pdo &gt;= 0.9.0)</p><p class="refpurpose"><span class="refname">PDOStatement::fetchColumn</span> &mdash; <span class="dc-title">
  从结果集中的下一行返回单独的一列。
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-pdostatement.fetchcolumn-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>PDOStatement::fetchColumn</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$column_number</code><span class="initializer"> = 0</span></span>): <span class="type">string</span></div>


  <p class="para rdfs-comment">
   从结果集中的下一行返回单独的一列，如果没有了，则返回 <strong><code>false</code></strong> 。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-pdostatement.fetchcolumn-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">column_number</code></dt>

     <dd>

      <p class="para">
       你想从行里取回的列的索引数字（以0开始的索引）。如果没有提供值，则 <span class="function"><strong>PDOStatement::fetchColumn()</strong></span> 获取第一列。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdostatement.fetchcolumn-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>PDOStatement::fetchColumn()</strong></span> 从结果集中的下一行返回单独的一列。
  </p>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    如果使用 <span class="function"><strong>PDOStatement::fetchColumn()</strong></span> 取回数据，则没有办法返回同一行的另外一列。
   </p>
  </div>
 </div>


 <div class="refsect1 examples" id="refsect1-pdostatement.fetchcolumn-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-1078"><p><strong>示例 #1 返回下一行的第一列</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$sth&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;name,&nbsp;colour&nbsp;FROM&nbsp;fruit"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/*&nbsp;从结果集中的下一行获取第一列&nbsp;&nbsp;*/<br /></span><span style="color: #007700">print(</span><span style="color: #DD0000">"从结果集中的下一行获取第一列：\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchColumn</span><span style="color: #007700">();<br />print(</span><span style="color: #DD0000">"name&nbsp;=&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #DD0000">\n"</span><span style="color: #007700">);<br /><br />print(</span><span style="color: #DD0000">"从结果集中的下一行获取第二列：\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchColumn</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />print(</span><span style="color: #DD0000">"colour&nbsp;=&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #DD0000">\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
从结果集中的下一行获取第一列：
name = lemon
从结果集中的下一行获取第二列：
colour = red
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-pdostatement.fetchcolumn-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="pdo.query.html" class="function" rel="rdfs-seeAlso">PDO::query()</a> - 执行 SQL 语句，以 PDOStatement 对象形式返回结果集</span></li>
    <li class="member"><span class="function"><a href="pdostatement.fetch.html" class="function" rel="rdfs-seeAlso">PDOStatement::fetch()</a> - 从结果集中获取下一行</span></li>
    <li class="member"><span class="function"><a href="pdostatement.fetchall.html" class="function" rel="rdfs-seeAlso">PDOStatement::fetchAll()</a> - 返回一个包含结果集中所有行的数组</span></li>
    <li class="member"><span class="function"><a href="pdo.prepare.html" class="function" rel="rdfs-seeAlso">PDO::prepare()</a> - 准备要执行的语句，并返回语句对象</span></li>
    <li class="member"><span class="function"><a href="pdostatement.setfetchmode.html" class="function" rel="rdfs-seeAlso">PDOStatement::setFetchMode()</a> - 为语句设置默认的获取模式。</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>